package ro.upt.ac.cstaicu.execution.testng;

import java.util.ArrayList;
import java.util.Collection;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import ro.upt.ac.cstaicu.execution.Executor;
import ro.upt.ac.cstaicu.execution.TestResult;
import ro.upt.ac.cstaicu.instrumentation.logging.PredicateCounterLogger;

public class TestNGListener implements ITestListener {

	private Collection<TestResult> results = new ArrayList<TestResult>();
	private String currentTestClass;
	private String currentTestMethod;
	
	public Collection<TestResult> getResults() {
		return results;
	}
	
	@Override
	public void onStart(ITestContext arg0) {
	
	}
	
	@Override
	public void onFinish(ITestContext arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onTestFailure(ITestResult arg0) {
//		TestResult res = new TestResult(currentTestClass, currentTestMethod,
//				Executor.TEST_FAILED, arg0.getThrowable().getCause().getClass().getSimpleName());
		TestResult res = new TestResult(currentTestClass, currentTestMethod,
				Executor.TEST_FAILED, arg0.getThrowable().getStackTrace()[0] + "");
		PredicateCounterLogger.getInstance().setResult(res);
		results.add(res);
		
	}
	
	@Override
	public void onTestStart(ITestResult arg0) {
		currentTestClass = arg0.getTestClass().getName();
		currentTestMethod = arg0.getMethod().getMethodName();
		PredicateCounterLogger.getInstance().testStarted(currentTestClass, currentTestMethod);
	}
	
	@Override
	public void onTestSkipped(ITestResult arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onTestSuccess(ITestResult arg0) {
		TestResult res = new TestResult(currentTestClass, currentTestMethod,
				Executor.TEST_PASSED, "");

		PredicateCounterLogger.getInstance().setResult(res);
		results.add(res);
		
	}
	
}
